//
//  baseViewController.m
//  watchApp
//
//  Created by jiangjinwei on 15/9/25.
//  Copyright (c) 2015年 koridy. All rights reserved.
//

#import "AppBaseViewController.h"

@interface AppBaseViewController ()<UIGestureRecognizerDelegate>

@end

@implementation AppBaseViewController

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:true];
    [self.navigationController.navigationBar setBackgroundColor:[UIColor clearColor]];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    self.automaticallyAdjustsScrollViewInsets = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = KColorBgGray;
    [self initLeftnavbtn];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.extendedLayoutIncludesOpaqueBars = NO;
    self.modalPresentationCapturesStatusBarAppearance = NO;
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    [self.navigationController.navigationBar setBarTintColor:MAIN_Color];
    [self setupBarButtonItemTheme];
    // Do any additional setup after loading the view.
}

-(void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:YES];
}

-(void)initLeftnavbtn
{
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    
    UIButton*btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    
    btn.frame = CGRectMake(0, 0, 20, 20);
    [btn setBackgroundImage:[UIImage imageNamed:@"back_icon"] forState:UIControlStateNormal];
    
    btn.adjustsImageWhenHighlighted = NO;
    [btn addTarget:self action:@selector(leftBTnCLick) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem*left = [[UIBarButtonItem alloc]initWithCustomView:btn];
    self.navigationItem.leftBarButtonItem = left;
    
    // 设置导航栏下面 线的颜色
    UINavigationBar *navigationBar = self.tabBarController.navigationController.navigationBar;
    [navigationBar setBackgroundImage:[ZQColorChangeImage createImageWithColor:KColorClear]
                       forBarPosition:UIBarPositionAny
                           barMetrics:UIBarMetricsDefault];
    [navigationBar setShadowImage:[ZQColorChangeImage createImageWithColor:KColorWhite]];
    
    // 设置导航栏标题 字体
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont systemFontOfSize:19],
       NSForegroundColorAttributeName:KColorWhite}];
}

-(void)leftBTnCLick
{
//    if ([self.navigationController respondsToSelector:@selector(popViewControllerAnimated:)]){
        [self.navigationController popViewControllerAnimated:YES];
//    }
    
    if([self respondsToSelector:@selector(dismissModalViewControllerAnimated:)]){
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:YES];
}
    

- (void)setupBarButtonItemTheme
{
    UIBarButtonItem *item = [UIBarButtonItem appearance];
    
    // 设置文字属性
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
    textAttrs[NSFontAttributeName] = FoneSize(15);
    [item setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
    
    NSMutableDictionary *highlightedTextAttrs = [NSMutableDictionary dictionary];
    highlightedTextAttrs[NSForegroundColorAttributeName] = RGB(213.0, 169.0, 51.0);
    [item setTitleTextAttributes:highlightedTextAttrs forState:UIControlStateHighlighted];
    
    NSMutableDictionary *disableTextAttrs = [NSMutableDictionary dictionary];
    disableTextAttrs[NSForegroundColorAttributeName] = RGB(239.0, 174.0, 0.0);
    [item setTitleTextAttributes:disableTextAttrs forState:UIControlStateDisabled];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

-(UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

@end
